package linkedlist;

public class SingleLinkedList {

    private SingleNode m_pNodeHead;
    private int m_iSize;

    public Object getByIndex(int iIndex) {
        if (iIndex > m_iSize - 1) {
            return null;
        }

        SingleNode pNodeTemp = m_pNodeHead;
        for (int i = 0; i < iIndex; i++) {
            pNodeTemp = pNodeTemp.getNodeNext();
        }

        return pNodeTemp;
    }

    public void insertToHead(Object objData) {
        SingleNode pNode = new SingleNode();
        pNode.setObjData(objData);
        if (this.m_iSize == 0) {
            this.m_pNodeHead = pNode;
        } else {
            pNode.setNodeNext(this.m_pNodeHead);
            this.m_pNodeHead = pNode;
        }
        this.m_iSize++;
    }

    public void insertTail(Object objData) {
        SingleNode pNode = new SingleNode();

        if (this.m_iSize == 0) {
            this.m_pNodeHead = pNode;
        } else {
            SingleNode pLastNode = this.m_pNodeHead;
            while (pLastNode.getNodeNext() != null) {
                pLastNode = pLastNode.getNodeNext();
            }
            pLastNode.setNodeNext(pNode);
        }
        this.m_iSize++;
    }

    public void insertAfter(Object objDest, Object objNew) {
        SingleNode pNodeNew = new SingleNode();
        pNodeNew.setObjData(objNew);

        if (this.m_pNodeHead == null) {
            this.m_pNodeHead = pNodeNew;

        } else {
            SingleNode pNodeDest = this.m_pNodeHead;
            while (pNodeDest.getNodeNext() != null) {
                if (pNodeDest.getObjData() == objDest) {
                    break;
                }

                pNodeDest = pNodeDest.getNodeNext();
            }
            SingleNode pNodeNext = pNodeDest.getNodeNext();
            pNodeNew.setNodeNext(pNodeNext);
            pNodeDest.setNodeNext(pNodeNew);
        }

        this.m_iSize++;
    }

    public void insertBefore(Object objDest, Object objNew) {

    }

    public void delete(Object objData) {

    }

    public void putAll() {

    }
}
